package com.zhz.lib.cms.conf;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.userdetails.UserDetails;

public class menuAction extends Action {

	/**
	 * 执行方法
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SecurityContext sc = SecurityContextHolder.getContext();
			Authentication auth = sc.getAuthentication();

			Object principal = auth.getPrincipal();
			Map<String, String> roleMap = new HashMap<String, String>();
			UserDetails userDetails;

			if (principal instanceof UserDetails) {
				userDetails = (UserDetails) principal;
				// 用户密码
				String password = userDetails.getPassword();
				// 用户名称
				String username = userDetails.getUsername();
				// 用户权限
				GrantedAuthority[] authorities = userDetails.getAuthorities();

				for (int i = 0; i < authorities.length; i++) {
					roleMap.put(authorities[i].getAuthority(), authorities[i]
							.getAuthority());
					request.setAttribute(authorities[i].getAuthority(),
							authorities[i].getAuthority());
				}
				request.setAttribute("userDetails", userDetails);
				request.setAttribute("roleMap", roleMap);
			}

			return mapping.findForward("menu");
		} catch (Exception e) {
			System.out.println(e.toString());
			return null;
		}
	}

}
